import java.awt.Rectangle;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
 * 
 *is the control class of Add Field window.
 */
public class AddFieldControl
{
	/** array of strings for data sent to database - for o_GetFields */
	public String[] searchData = new String[3]; 
	public String[] searchData2 = new String[2]; 
	/** array of strings for data sent to database - for o_AddField */
	public String[] FieldData = new String[4];
	/** array of strings for result from database - for o_GetFields */
	public String[] res;
	/** strings for result from database - for others */
	public String Answer;
	
	/**
	 * this method collects needed info from gui, and sends the DB a request to add the
	 *	 entered Field  (if already exists notify to user)	 
	 */
	public void  o_AddField(AddFieldGui FieldGui) 
	{
		Answer=new String();
		FieldData=new String[3];
		FieldData[0]=Main.gusers.id;
		FieldData[1]="addField";
		FieldData[2]=FieldGui.SubjectText.getText();		
		if(FieldData[2].equals(""))
    	{
    		 JOptionPane.showMessageDialog(null,"Field is empty, Please try again !","Input error",JOptionPane.ERROR_MESSAGE);
    		 return;
    	 }
		Main.log.connection.accept("addField");
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		Answer=ChatClient.getRs();
		if(Answer.equals("Exists"))
		{
			JOptionPane.showMessageDialog(null,"The entered Field '"+FieldData[2]+"' already exists !","Database Conflict",JOptionPane.ERROR_MESSAGE);
			
		}
		else if(Answer.equals("OK"))
		{
			JOptionPane.showMessageDialog(null,"Field Succesfully added to the Database","Database Success",JOptionPane.INFORMATION_MESSAGE);
		}
		o_Back(FieldGui.panel);
	}
		/**
	 * this method collects needed info from gui, and sends the DB a request to Remove the
	 *	 Selected Field  (if already exists notify to user)	 
	 */
	public void  o_RemoveField(AddFieldGui FieldGui) 
	{
		Answer=new String();	
		FieldData=new String[3];
		FieldData[0]=Main.gusers.id;
		FieldData[1]="RemoveField";		
		FieldData[2]=(String) FieldGui.FieldCombo2.getSelectedItem();			
		Main.log.connection.accept("RemoveField");
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;
		Answer=ChatClient.getRs();
		if(Answer.equals("Exists"))
			JOptionPane.showMessageDialog(null,"A book Exists in Database for The entered Field: '"+FieldData[2]+"'" +" !\n Can't remove a subject with related books","Database Conflict",JOptionPane.ERROR_MESSAGE);		
		else if(Answer.equals("OK"))
				JOptionPane.showMessageDialog(null,"Field Succesfully Removed from Database, including all its subjects","Database Success",JOptionPane.INFORMATION_MESSAGE);
		o_Back(FieldGui.panel);   
	}
	
	/**
	 * this method and sends the DB a request to Retreive Field list
	 *	 updates the Gui Fields Combobox with the Result list 
	 */
	public void  o_GetFields(AddFieldGui FieldGui) 
	{
		String temp;
		searchData = new String[3];
		searchData[0]=Main.gusers.id;		 
		searchData[1]="getFields2";			
		Main.log.connection.accept(searchData[1]);
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		temp=ChatClient.getRs();
		res=temp.split("\\*");
		if (FieldGui.FieldCombo2 == null) {
			FieldGui.FieldCombo2 = new JComboBox(res);				
			FieldGui.FieldCombo2.setBounds(new Rectangle(130, 290, 100, 36));
		}		
		FieldGui.panel.add(FieldGui.FieldCombo2,null);
		FieldGui.panel.updateUI();		
	}
	/**
	 * this method Goes back to the last Gui Screen - Book Management
	 */	
	public void o_Back(JPanel panel) 
	{
		panel.setVisible(false);
		panel.setEnabled(false);
		Main.log.frame.setSize(550,515);
		Main.bookManagement.panel.setVisible(true);
		Main.bookManagement.panel.setEnabled(true);
		
	}
	/**
	 * this method logs out of the system
	 */	
	public void o_logout(JPanel BookMngpanel)
	{
		if(JOptionPane.showConfirmDialog(null, "Are you sure you want to logout? ","Confirmation" ,JOptionPane.OK_CANCEL_OPTION) == 0)
		{
			BookMngpanel.setEnabled(false);
			BookMngpanel.setVisible(false);
			Main.log.loginData = new String[3];
			Main.log.loginData[0]=Main.gusers.id;
			Main.log.loginData[1]="logout";
			Main.log.loginData[2]=Main.gusers.id;		
			Main.log.connection= new ClientQuery(Main.serverIP,5555);
			Main.log.connection.accept("logout");
			Main.log.user.setText("");
			Main.log.pass.setText("");
			Main.log.frame.setSize(650,550);
			Main.log.panel.setEnabled(true);
			Main.log.panel.setVisible(true);
		}
	}


}
